package com.common.util.repay_plan;

import com.common.util.date.DateUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

/**
 * @describe: 到期还款付息
 */
class DebtServiceDue {
    
    /**
     * 到期后收到的本息 = 借款金额 * (1 + 借款年/年化天数 * 借款天数)
     * @param repayPlanCondition 还款计划条件
     * @return
     */
    protected static ArrayList<RepayPlan> getPlan(RepayPlanCondition repayPlanCondition) {
        Date startTime = repayPlanCondition.getStartTime();  //计息开始时间
        int loanDays = repayPlanCondition.getLoanDays();     //借款天数
        BigDecimal amt = repayPlanCondition.getAmt();      //借款总金额
        BigDecimal rate = repayPlanCondition.getRate();      //年化利率
        int annualNumberOfDays = repayPlanCondition.getAnnualNumberOfDays();  //年化天数
        
        ArrayList<RepayPlan> repayPlanList = new ArrayList<>();
        RepayPlan repayPlan = new RepayPlan();
        repayPlan.setStartDate(startTime);
        Date endTime = DateUtil.getEndTime(DateUtil.addDay(startTime, loanDays - 1));
        repayPlan.setEndDate(endTime);
        repayPlan.setPrincipal(amt);
        BigDecimal interest = amt.multiply(BigDecimal.valueOf(loanDays))
                .multiply(rate)
                .divide(BigDecimal.valueOf(annualNumberOfDays), 2, BigDecimal.ROUND_HALF_UP);
        repayPlan.setInterest(interest);
        repayPlan.setLastPrincipal(amt);
        repayPlan.setInterestDays(loanDays);
        repayPlanList.add(repayPlan);
        return repayPlanList;
    }
}
